package com.kamistoat.netty.zerocopy_old;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 传统IO文件传输客户端
 */
public class OldIOClient {
    public static void main(String[] args) {
        try {
            // 建立链接
            Socket socket = new Socket("127.0.0.1", 7001);
            // 一个1.9MB的zip文件
            FileInputStream fileInputStream =
                    new FileInputStream("/home/kamistoat/ubuntu/Netty_Learning/src/main/java/com/kamistoat/netty/zerocopy_old/spring-boot-2.4.2.zip");
            // 获取socket中的输出流
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

            byte[] bytes = new byte[4096];
            long readCount;
            long total = 0;
            long startTime = System.currentTimeMillis();

            // 循环冲文件输入流中读取数据，直到读到-1
            while((readCount = fileInputStream.read(bytes)) >= 0){
                total += readCount;
                dataOutputStream.write(bytes);
            }

            System.out.println("总字节数: " + total + ", 耗时: " + (System.currentTimeMillis() - startTime));

            dataOutputStream.close();
            socket.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
